const mysql = require("mysql");
// 创建 mysql 连接池资源
const pool = mysql.createPool({
  host: "localhost",
  user: "root",
  password: "xxxx",
  database: "test",
});

exports.query = //promise化mysql查询语句，优化代码可阅读性
  function (sql, sqldata) {
    return new Promise(function (resolve, reject) {
      pool.getConnection(function (err, connection) {
        connection.query(sql, sqldata, function (err, result) {
          if (err) {
            console.log(err);
            reject("sql查询错误了");
          } else {
            resolve(result);
          }
          connection.release();
          //连接不再使用，返回到连接池
        });
      });
    });
  };
